/******************** 
	作用:粉丝分组
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-25
	
********************/

$(function() {
	var tableId = "#dynamic-table"; //表格ID
	var addUrl = "/UserCenter/Group/add"; //添加分组的网址
	var editUrl = "/UserCenter/Group/update"; //编辑分组的网址
	var synchronousUrl = "/Sync/GroupSync"; //同步分组的网址
	var deleteUrl = "/UserCenter/Group/delete"; //删除的地址


	General.extendDialog(); //扩展对话框



	//---------------------------同步分组
	//点击"同步分组"按钮
	$("#synchronous").on("click", function(e) {
		synchronousGroup();
	});
	/**
	 * 同步分组
	 */
	function synchronousGroup() {
		var options = {};
		var url = synchronousUrl;
		var data = {};
		var tip = "正在同步分组";
		var method = 'get';
		Common.ajaxPost(options, beforeSynchronousGroup, url, data, tip, completeSynchronousGroup, failSynchronousGroup,method);
	};
	//同步分组前的操作
	function beforeSynchronousGroup() {
		//		General.showMask();
	};
	//同步分组成功
	function completeSynchronousGroup(options, data, status) {

		data=$.parseJSON(data);
		if(data.success){
			alert('同步完成,总共耗时['+data.cost_time+']s');
			General.hideMask();
			General.refresh(); //刷新页面
		}else{
			failSynchronousGroup(data);
		}
		
	};
	//同步分组失败
	function failSynchronousGroup(data) {
		General.showMask();
		alert("同步分组失败["+data.msg+"]");
		General.hideMask();
	};
	//---------------------------添加分组
	//点击"添加分组"按钮
	$("#add").on("click", function(e) {
		$("#dialog-edit").html($("#tpl-addGroup").html());
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-plus green'></i> 添加分组</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text]").val(); //获取分组名称
					if (!result) {
						alert("请输入分组名称!");
						$("#dialog-edit input[type=text]").focus();
						return;
					}
					var options = {};
					var url = addUrl;
					var data = {
						"groupName": result
					};
					var tip = "正在添加分组";
					Common.ajaxPost(options, beforeAdd, url, data, tip, completeAdd, failAdd);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//添加分组前的操作
	function beforeAdd() {
		General.closeEditWindow();
		General.showMask();
	};
	//添加分组成功
	function completeAdd(options, data, status) {
		
		data=$.parseJSON(data);		
		//console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			if (data.msg) {
				alert(data.msg);
			} else {
				alert(data.sub_msg);
			}	
			
			General.hideMask(); 
		}
		
	};
	//添加分组失败
	function failAdd() {
		
		alert('添加分组失败！');
		General.hideMask();
		
	};
	//---------------------------编辑分组
	//点击行内"编辑"按钮
	$(tableId + ">tbody a[data-id=edit]:not([disabled])").on("click", function(e) {
		$("#dialog-edit").html($("#tpl-editGroup").html());
		var tr=$(this).closest("tr");
		var gid = tr.attr("data-id"); //获取gid
		var text = tr.find("td:eq(1)").text();
		$("#dialog-edit input[type=text]").val(text); //设置分组名称
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-edit green'></i> 编辑分组</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text]").val(); //获取分组名称
					if (!result) {
						alert("请输入分组名称!");
						$("#dialog-edit input[type=text]").focus();
						return;
					}
					var options = {};
					var url = editUrl;
					var data = {
						"gid": gid,
						"groupName": result
					};
					var tip = "正在编辑分组";
					
					Common.ajaxPost(options, beforeEdit, url, data, tip, completeEdit, failEdit);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//编辑分组前的操作
	function beforeEdit() {
		General.closeEditWindow();
		General.showMask();
	};
	//编辑分组成功
	function completeEdit(options, data, status) {
		
		data=$.parseJSON(data);		
		console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			
			alert(data.sub_msg);
			General.hideMask(); 
			
		}
	};
	//编辑分组失败
	function failEdit() {
		
		alert("编辑分组失败!");
		General.hideMask();
		
	};
	//-----------------删除-----------------
	//点击删除所选按钮
	/*$("#delete").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var selectedRows = $(tableId+' > tbody input[type=checkbox]:checked');
		if (selectedRows.length > 0) {
			//弹出确认框
			if (confirm("您确定要删除所有选中的分组吗?")) {
				var ids = [];
				var id;
				selectedRows.each(function(index, checkbox) {
					id = $(this).closest("tr").attr("data-id");
					ids.push(id);
				});
				startDelete(ids); //开始删除
			}
		} else {
			General.alert("请先选中要删除的分组再进行操作!");
		}
	});*/
	//点击行内"删除"按钮
	$(tableId + ">tbody a[data-id=delete]:not([disabled])").on("click", function(e) {
		var tr = $(this).closest("tr");
		var id = tr.attr("data-id");
		var text = tr.find("td:eq(1)").text();
		if (confirm("您确定要删除分组[" + text + "]吗?")) {
			//		if (confirm("您确定要删除该分组吗?")) {
			startDelete([id]); //开始删除消息
		}
	});
	//开始删除
	function startDelete(ids) {
		General.showMask();
		var options = {
			"id": ids
		};
		var url = deleteUrl;
		var data = {
			"gid": ids.join(",")
		};
		var tip = "正在删除";
		Common.ajaxPost(options, null, url, data, tip, completeDelete, failDelete);
	};
	//删除成功
	function completeDelete(options, data, status) {
		
		data=$.parseJSON(data);		
		console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			window.location.href='/UserCenter/Group';
		}else if(data.error){
			
			alert(data.sub_msg);
			General.hideMask(); 
			
		}
	};
	//删除失败
	function failDelete() {
		alert("删除失败!");
		General.hideMask();
		
	};
});
